import { useMemberStore } from "@/store/modules/member.ts";
/* 全局未登录拦截 */
const loginPage = "/pages/login/login";
const whiteList = ['/pages/login/login', '/pages/index/index', '/pages/categor/categor',
 '/pages/goods/goods', '/pages/hot/hot','/pages/login/forget','/pages/login/register'];

function CheckWhite(url : string) {
	const path = url.split('?')[0];
	return whiteList.indexOf(path) !== -1;
}

let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];

list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(e) {
			const memberStore = useMemberStore(); // 在需要时获取memberStore  
			const token = memberStore.user.token;
			// const userId = memberStore.user.userId
			//有token执行获取用户信息的方法,这样写对不对？
			// if(token){
			// 	memberStore.getUserInfo(userId);
			// }

			if (!token && !CheckWhite(e.url)) {
				uni.showToast({
					icon: 'none',
					title: '你还未登录'
				})
				uni.navigateTo({
					url: loginPage
				});
				return false;
			}
			return true;
		},
		success(e) {
			console.log('addInterceptor.success', e);
		},
		fail(e) {
			console.log('addInterceptor.fail', e);
		}
	});
});